package edu.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ExecuteSysRuntimeCmdUtil {
	
	public Runtime getSysRuntime(){
		return Runtime.getRuntime();
	}

	public Process runCmd(String cmdStr){
		Process process=null;
		try {
			process=this.getSysRuntime().exec(cmdStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return process;
	}
	
	public static void main(String[] agrs) throws Exception{
		 String cmd = "ping www.baidu.com";
		 ExecuteSysRuntimeCmdUtil exeCmd=new ExecuteSysRuntimeCmdUtil();
		 Process p =exeCmd.runCmd(cmd);
		 BufferedInputStream in = new BufferedInputStream(p.getInputStream());  
         BufferedReader inBr = new BufferedReader(new InputStreamReader(in));  
         String lineStr;  
         while ((lineStr = inBr.readLine()) != null)  
             //获得命令执行后在控制台的输出信息  
             System.out.println(lineStr);// 打印输出信息  
         //检查命令是否执行失败。  
         if (p.waitFor() != 0) {  
             if (p.exitValue() == 1)//p.exitValue()==0表示正常结束，1：非正常结束  
                 System.err.println("命令执行失败!");  
         }  
         inBr.close();  
         in.close();  
	}
}
